package com.party.web.biz.wechat;

import com.party.api.dto.wechat.requst.MaterialListRequest;
import com.party.api.dto.wechat.response.MaterialListResponse;
import com.party.api.service.wechat.IWechatService;
import com.party.core.exception.BusinessException;
import com.party.core.model.member.MemberExternalApp;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.wechatPlatform.WechatOpenMember;
import com.party.core.service.member.IMemberExternalAppService;
import com.party.core.service.wechat.IWechatMaterialService;
import com.party.core.service.wechatPlatform.IWechatOpenMemberService;
import com.party.web.utils.RealmUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * 微信素材业务接口
 */
@Service
public class WechatMaterialBizService {

    private static Logger logger = LoggerFactory.getLogger(WechatMaterialBizService.class);

    @Autowired
    private IWechatOpenMemberService wechatOpenMemberService;

    @Autowired
    private IWechatService wechatService;

    @Autowired
    private IWechatMaterialService wechatMaterialService;

    @Autowired
    private IMemberExternalAppService memberExternalAppService;

    //获取永久素材列表
    public MaterialListResponse batchGetMaterial(MaterialListRequest req) {
        //获取accesson_token
        MemberGroup member = RealmUtils.getCurrentUser();
        checkNotNull(member, "当前用户为空");
        //WechatOpenMember wechatOpenMember = wechatOpenMemberService.findByMemberId(member.getId());
        MemberExternalApp memberExternalApp
                = memberExternalAppService.findByMemberIdAndDeployType(member.getId(), member.getDeployType());
        WechatOpenMember wechatOpenMember = wechatOpenMemberService.get(memberExternalApp.getExternalAccountId());
        if (null == wechatOpenMember) {
            throw new BusinessException("授权公众号不能为空");
        }
        logger.info("access token:{}",wechatOpenMember.getAuthorizerAccessToken());
        MaterialListResponse resp = wechatService.getMaterialList(req,wechatOpenMember.getAuthorizerAccessToken());
        return resp;
    }

    //获取指定的永久素材
//    public NewsItemResponse getNewsMaterial(String mediaId){
//        //获取accesson_token
//        MemberGroup member = RealmUtils.getCurrentUser();
//        checkNotNull(member, "当前用户为空");
//        WechatOpenMember wechatOpenMember = wechatOpenMemberService.findByMemberId(member.getId());
//        if (null == wechatOpenMember) {
//            throw new BusinessException("授权公众号不能为空");
//        }
//        MaterialRequest req = new MaterialRequest();
//        req.setMediaId(mediaId);
//        NewsItemResponse resp = materialService.getNewsMaterial(req,wechatOpenMember.getAuthorizerAccessToken());
//        return resp;
//    }
//
//    //删除永久素材
//    public boolean deleteMaterial(String mediaId) {
//        //获取accesson_token
//        MemberGroup member = RealmUtils.getCurrentUser();
//        checkNotNull(member, "当前用户为空");
//        WechatOpenMember wechatOpenMember = wechatOpenMemberService.findByMemberId(member.getId());
//        if (null == wechatOpenMember) {
//            throw new BusinessException("授权公众号不能为空");
//        }
//
//        //调用微信接口
//        MediaIdOutput mediaIdOutput = new MediaIdOutput();
//        mediaIdOutput.setMediaId(mediaId);
//        boolean opFlag = wechatTmpService.delMaterial(mediaIdOutput, wechatOpenMember.getAuthorizerAccessToken());
//        return opFlag;
//    }
//
//    //新增永久图文素材
//    public String addNews(NewsOutput input) {
//        //获取accesson_token
//        MemberGroup member = RealmUtils.getCurrentUser();
//        checkNotNull(member, "当前用户为空");
//        WechatOpenMember wechatOpenMember = wechatOpenMemberService.findByMemberId(member.getId());
//        if (null == wechatOpenMember) {
//            throw new BusinessException("授权公众号不能为空");
//        }
//
//        //调用微信接口
//        String mediaId = wechatTmpService.addNews(input, wechatOpenMember.getAuthorizerAccessToken());
//        return mediaId;
//    }
//
//    //修改永久图文素材
//    public boolean updateNews(NewsOutput output) {
//        //获取accesson_token
//        MemberGroup member = RealmUtils.getCurrentUser();
//        checkNotNull(member, "当前用户为空");
//        WechatOpenMember wechatOpenMember = wechatOpenMemberService.findByMemberId(member.getId());
//        if (null == wechatOpenMember) {
//            throw new BusinessException("授权公众号不能为空");
//        }
//
//        //调用微信接口
//        boolean opFlag = wechatTmpService.updateNews(output, wechatOpenMember.getAuthorizerAccessToken());
//        return opFlag;
//    }
//
//    //上传图文消息内的图片
//    public String uploadImg(File file){
//        //获取accesson_token
//        MemberGroup member = RealmUtils.getCurrentUser();
//        checkNotNull(member, "当前用户为空");
//        WechatOpenMember wechatOpenMember = wechatOpenMemberService.findByMemberId(member.getId());
//        if (null == wechatOpenMember) {
//            throw new BusinessException("授权公众号不能为空");
//        }
//
//        String url = wechatTmpService.uploadImg(file,wechatOpenMember.getAuthorizerAccessToken());
//        return url;
//    }
//
//    //新增其它类型永久素材
//    public MaterialInput addMaterial(File file, String type) {
//        //获取accesson_token
//        MemberGroup member = RealmUtils.getCurrentUser();
//        checkNotNull(member, "当前用户为空");
//        WechatOpenMember wechatOpenMember = wechatOpenMemberService.findByMemberId(member.getId());
//        if (null == wechatOpenMember) {
//            throw new BusinessException("授权公众号不能为空");
//        }
//
//        MaterialInput input = wechatTmpService.addMaterial(file, type, wechatOpenMember.getAuthorizerAccessToken());
//        return input;
//    }

}
